import Taro from '@tarojs/taro'
import { stringify } from 'qs';
import { prefix } from '../utils'

export function add(data, callback) {
    return () => {
        const userInfo = Taro.getStorageSync('userInfo')
        const payload = { ...data, ...userInfo }

        Taro.request({
                url: `${prefix}/api/v1/goods`,
                data: payload,
                method: 'POST',
                header: { 'content-type': 'application/json' }
            })
            .then(results => {
                const { data = {} } = results || {}
                if (callback) callback(data)
            })

    }
}

export function queryList(params = {}) {
    return dispatch => {

        Taro.request({
                url: `${prefix}/api/v1/goods?${stringify(params)}`,
                method: 'GET',
                header: { 'content-type': 'application/json' }
            })
            .then(results => {
                const { data = {} } = results || {}
                dispatch({
                    type: 'lists/list',
                    payload: { data, isLoading: false, isLoadMore: false, }
                })
            })

    }
}

export function queryProfile(params, callback) {
    return dispatch => {

        Taro.request({
                url: `${prefix}/api/v1/goods/${params.id}`,
                method: 'GET',
                header: { 'content-type': 'application/json' }
            })
            .then(results => {
                const { data = {} } = results || {}
                dispatch({
                    type: 'lists/profile',
                    payload: { profile: data, isLoading: false, isLoadMore: false, }
                })
                if (callback) callback(data)
            })

    }
}

export function queryUsers({ id, ...params }, callback) {
    return dispatch => {

        Taro.request({
                url: `${prefix}/api/v1/goods/${id}/users?${stringify(params)}`,
                method: 'GET',
                header: { 'content-type': 'application/json' }
            })
            .then(results => {
                const { data = {} } = results || {}
                dispatch({
                    type: 'lists/users',
                    payload: { users: data }
                })
                if (callback) callback(data)
            })
            .catch(e => callback(e))

    }
}

export function queryIntegrals(params, callback) {
    return dispatch => {

        Taro.request({
                url: `${prefix}/api/v1/goods/${params.id}/integrals`,
                method: 'GET',
                header: { 'content-type': 'application/json' }
            })
            .then(results => {
                const { data = {} } = results || {}
                dispatch({
                    type: 'lists/integralIns',
                    payload: { integralIns: data }
                })
                if (callback) callback(data)
            })

    }
}

export function createProfile(params, callback) {
    return dispatch => {
        const userInfo = Taro.getStorageSync('userInfo')

        Taro.request({
                url: `${prefix}/api/v1/users/${userInfo.userId}/goods`,
                method: 'POST',
                header: { 'content-type': 'application/json' },
                data: { userId: userInfo.userId, goodsId: params.id }
            })
            .then(results => {
                const { data = {} } = results || {}
                dispatch({
                    type: 'lists/integralIns',
                    payload: { integralIns: data }
                })
                if (callback) callback(data)
            })

    }
}